home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / cshx86.zip / SAMPLES / DUMPENV.C < prev    next >
C/C++ Source or Header  |  1993-04-14  |  3KB  |  89 lines

  1. /***************************************************************************/
  2. /*                                                                                                    */
  3. /*                                                                                                    */
  4. /*            Dump the current environmentals as setenv statements.                    */
  5. /*    Copyright (c) 1989-1992 by Hamilton Laboratories.  All rights reserved.    */
  6. /*                                                                                                    */
  7. /*                                                                                                    */
  8. /***************************************************************************/
  9.         
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13.  
  14. /*    This little utility will dump out your environmental variables in a
  15.     form that can be executed in a login.csh file.  It looks for any
  16.     characters that need enclosing single quotes and uses the individual
  17.     escapesym character (assumed to be '^') to escape any that require
  18.     more than just single quotes.
  19.     
  20.     (escapesym chars, assumed to be '^', are not further quoted; we assume
  21.     if this appears, it's probably meant to be a quote of the next character.)
  22.  
  23.     When it writes out the PATH variable, it inserts ".;", representing
  24.     the current directory to the start of the string if it's not already
  25.     there.  This is a convenience in converting from cmd.exe's convention
  26.     of always trying the current directory first to csh.exe's of trying
  27.     the current directory only if you specify it.  (You can conditionally
  28.     compile-out this option by #defining TruePATH.)
  29.  
  30.     */
  31.  
  32. char chars_needing_quotes[] = "!\n\r'\"` &|;<>()*?{}[]~$";
  33.  
  34. void cdecl main ( int argc, char *argv[], char *envp[] )
  35.         {
  36.         register char *e;
  37.         int i;
  38.         for (i = 0;  envp[i]; i++)
  39.             {
  40.             for (e = envp[i];  *e && !strchr(chars_needing_quotes, *e);  e++)
  41.                 ;
  42.             if (*e)
  43.                 {
  44.                 /*    Enclose the value in quotes. */
  45.                 e = strchr(envp[i], '=');
  46.                 *e = '\0';
  47.                 printf ("setenv %s='", envp[i]);
  48.  
  49. #                ifndef TruePATH
  50.                     if (strcmp(envp[i], "PATH") == 0 &&
  51.                             (e[1] != '.' || e[2] != ';'))
  52.                         {
  53.                         /*    PATH variable doesn't already start with ".;" */
  54.                         putchar('.');
  55.                         putchar(';');
  56.                         }
  57. #                endif
  58.  
  59.                 while (*++e)
  60.                     switch (*e)
  61.                         {
  62.                         case '!':
  63.                         case '"':
  64.                         case '\'':
  65.                         case '`':
  66.                             putchar('^');
  67.                             putchar(*e);
  68.                             break;
  69.                         case '\n':
  70.                             putchar('^');
  71.                             putchar('n');
  72.                             break;
  73.                         case '\r':
  74.                             putchar('^');
  75.                             putchar('r');
  76.                             break;
  77.                         default:
  78.                             putchar(*e);
  79.                         }
  80.                 fputs("'\n", stdout);
  81.                 }
  82.             else
  83.                 /*    Ignore the possibility of a PATH variable that didn't have at
  84.                     least one semicolon. */
  85.                 printf ("setenv %s\n", envp[i]);
  86.             }
  87.         exit(0);
  88.         }
  89.